package dang.action.user;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import dang.common.ImageUtil;

public class ImageAction {
	private InputStream imgstream;

	public String execute(){
		Map<String, BufferedImage> map=ImageUtil.getImage();
		//获取验证图片上的字符，保存到session
		String key=map.keySet().iterator().next();
		Map<String,Object> session=ActionContext.getContext().getSession();
		session.put("validateCode", key);
		//获取验证图片，以stream方式响应
		BufferedImage image = map.get(key);
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		JPEGImageEncoder jpeg=JPEGCodec.createJPEGEncoder(bos);
		try {
			jpeg.encode(image);
			byte[] bts=bos.toByteArray();
			imgstream=new ByteArrayInputStream(bts);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public InputStream getImgstream() {
		return imgstream;
	}

	public void setImgstream(InputStream imgstream) {
		this.imgstream = imgstream;
	}
	
}
